/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Exercise1Project.Models.Validation; // added namespace reference


namespace Exercise1Project.Models
{
    public class BikeServiceBusinessLogic : IBikeServiceBusinessLogic
    {
        private IValidationDictionary _validationDictionary;
        private IBikeServiceRepository _repository;

        public BikeServiceBusinessLogic(IValidationDictionary validationDictionary) : this(validationDictionary, new BikeServiceRepository())
        {

        }

        public BikeServiceBusinessLogic(IValidationDictionary validationDictionary, IBikeServiceRepository repository)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
        }

        // Validation business Logic
        public bool ValidateBikeService (BikeService bikeServiceToValidate)
        {
           // if (bikeServiceToValidate.SelectedItems == null || bikeServiceToValidate.SelectedItems.Trim().Length == 0 || bikeServiceToValidate.SelectedItems.Trim().Length > 300)                      // null or trim().length checks if null or empty
           //     _validationDictionary.AddError("SelectedItems", "Required Field!- less than 300 characters");
           // if (bikeServiceToValidate.StatusBikeInShop == null || bikeServiceToValidate.StatusBikeInShop.Trim().Length == 0 )
           //     _validationDictionary.AddError("StatusBikeInShop", "Required Field!");
           // if (bikeServiceToValidate.StatusService == null || bikeServiceToValidate.StatusService.Trim().Length == 0)
            //    _validationDictionary.AddError("StatusService", "Required Field!");

            return _validationDictionary.IsValid;
        }



        //Database Business Logic
        public IEnumerable<BikeService> GetAllBikeServices()
        {
            return _repository.GetAllBikeServices();
        }

        public IEnumerable<BikeService> GetAllBikeServicesByBikeInShopStatus(string bikeinshopstatus)
        {
            return _repository.GetAllBikeServicesByBikeInShopStatus(bikeinshopstatus);
        }

        public IEnumerable<BikeService> GetAllBikeServicesByServiceStatus(string bikeservicestatus)
        {
            return _repository.GetAllBikeServicesByServiceStatus(bikeservicestatus);
        }

        public IEnumerable<BikeService> GetAllBikeServicesByInShopAndServiceStatus(string bikeservicestatus, string bikeinshopstatus)
        {
            return _repository.GetAllBikeServicesByInShopAndServiceStatus(bikeservicestatus, bikeinshopstatus);
        }

        public IEnumerable<BikeService> GetBikeServiceByBikeID(int id)
        {
            return _repository.GetBikeServiceByBikeID(id);
        }

        public bool AddBikeService(BikeService bikeServiceToAdd)
        {
            //Validation
           // if (!ValidateBikeService(bikeServiceToAdd))
            //    return false;
            
            //Database
            try
            {
                _repository.AddBikeService(bikeServiceToAdd);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool UpdateBikeService(BikeService bikeServiceToUpdate)
        {
            //Validation
            if (!ValidateBikeService(bikeServiceToUpdate))
                return false;

            //Database
            try
            {
                _repository.UpdateBikeService(bikeServiceToUpdate);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool RemoveBikeService(int id)
        {
            //Validation
              // validation not checked for a remove or delete

            //Database
            try
            {
                _repository.RemoveBikeService(id);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public BikeService GetBikeServiceByServiceId(int id)
        {
            return _repository.GetBikeServiceByServiceId(id);
        }

        public IEnumerable<BikeService> returnAdminSearchQuery(BikeService model)
        {
            if (model.BikeServiceStatus == "All")  // search all bike service statuses
            {
                if (model.BikeInStoreStatus == "All")  // use all bike in store statuses
                {
                    var query1 = _repository.GetAllBikeServices();
                    return query1;
                }
                else                            // only use selected bike in store status
                {
                    var query2 = _repository.GetAllBikeServicesByBikeInShopStatus(model.BikeInStoreStatus);
                    return query2;
                }
            }
            else // search only selected bike service status
            {
                if (model.BikeInStoreStatus == "All")  // use all bike in store statuses
                {
                    var query3 = _repository.GetAllBikeServicesByServiceStatus(model.BikeServiceStatus);
                    return query3;
                }
                else                            // only use selected bike in store status
                {
                    var query4 = _repository.GetAllBikeServicesByInShopAndServiceStatus(model.BikeServiceStatus, model.BikeInStoreStatus);
                    return query4;
                }

            }
        }


    }
}
